
		**********************************************************
		** File: Table_3_merging_data.do						**
		** Paper: Human Trafficking Indicators: A New Dataset	**
		** Author: Richard Frank								**
		** Date: June 25, 2021									**
		** Task: merging various datasets used in EBA models 	**
		**********************************************************

		clear all
		version 16.1
		set seed 1234
	
		**************************************
		** change "~" to your working directory **
		** Please change the location below to where all the below 
		** raw files are located. I want to make sure that
		** the authors of the datasets below receive full credit for their work.
		** Therefore, I have included links to where the datasets I used can be
		** found without including them in the replication data. 

		cd "/~~~/"
					
 
					***************************				
					** INDEPENDENT VARIABLES **				
					***************************
					
					
	***************************************
	** Robust push factors (Cho 2015: 8) **
	***************************************
	
	** sources taken from Cho 2015 Appendix B **
		
		** 1. log GDP pc (World Bank)**
		** 2. Information flows (Dreher 2006) **
		** 3. Transition economy (OECD) **
		** 4. Muslim share (Encyclopedia Brittanica) **	
		** 5. Fertility rate (World Bank))**	
		** 6. Food, beverage and tobacco industries (World Bank)**	
		** 7. Rule of law ( Intl Country Risk Guide Kaufmann et al. 2009) **	
		** 8. Control of corruption ( Intl country risk guide, kaufmann et al 2009)**	  
		** 9. Infant mortality rate (World Bank) **	
		** 10. Share of age group 0-14 in population (World Bank)**	
		** 11. Crime rate (United Nations) **	
		** 12. Europe and Central Asia (World Bank) **	
		** 13. North Africa and Middle East (World Bank)**
	
	***************************************************************
	** Robust pull factors not in push factors (Cho Table 2: 10) **
	***************************************************************
		
		** 14. Language fractionalization (alesina et al)**	
		** 15. % of workforce in agriculture (World Bank)	
		** 16. refugees (World Bank))	
		** 17. log population (World Bank)	
		** 18. International tourism (World Bank)**		
		** 19. log amount of heroin seized (United Nations)**	
		** 21. OECD membership	
		** 22. East Asia and Pacific (World Bank)	
		** 23. Landlocked country (Mayer and Zignago 2011)	
		** 24. Share of Catholic in population (encylcopedia brittanica)
	
					************************************	
					** PREPARING DATASETS FOR MERGING **
					************************************
	
	** DREHER et al 2006 **
	
		** 2. Information flows **
		** Data download location:
		** https://kof.ethz.ch/en/forecasts-and-indicators/indicators/kof-globalisation-index.html
		use "KOFGI_2019_data.dta", clear
		
		keep country year KOFInGI KOFSoGI
		rename country Country
		run "cow.do"
		sort ccode year
		tab Country if ccode==0
		browse if ccode==ccode[_n-1] & year==year[_n-1]
		browse if ccode==710
		drop if ccode==0
		collapse (min)  KOFInGI KOFSoGI, by(ccode year)
		label var KOFInGI "Information flows (KOF)"
		label var KOFSoGI "Social globalization index (KOF)"
		save "KOF info index 2019.dta", replace
		
	** QoG Institute **
		
		** 4. Muslim share  **	
		** 24. Share of Catholic
		** Alesina et al ** 
		** 14. Language fractionalization **
		
		** Used data from QoG dataset **
		** Data download location:
		** https://www.gu.se/en/quality-government/qog-data/data-downloads/standard-dataset
		
		use "qog_std_ts_jan20.dta", clear
		keep  cname year ccodecow 	lp_muslim80 lp_no_cpm80 lp_protmg80 lp_catho80 al_language
		rename ccodecow ccode
		tab cname if ccode==ccode[_n-1] & year==year[_n-1]
		sort ccode year
		drop if ccode==.
		
		save "rel_lan.dta", replace

	** 23. Landlocked country **	
		
		** Used data from GeoDist dataset **
		** Data download location:	
		** http://www.cepii.fr/CEPII/en/bdd_modele/presentation.asp?id=6
		** download link on right side of page 
		
		use "geo_cepii.dta"	, clear
		keep country landlocked
		rename country Country
		run "cow.do"
		sort ccode
		drop if ccode==0
		order Country ccode
		rename Country country
		browse if ccode==ccode[_n-1]
		drop if country=="Western Sahara"
		drop if country=="Palestine"
		drop if country=="Hong Kong"	
		collapse (first) country (mean) landlocked , by(ccode)
		save "landlocked.dta", replace
 	
		** Freedom in the World **
		run "cleaning fh.do"
		
		** Transparency International **
		run "cleaning cpi.do"
		
		** UNODC **
		run "cleaning UNODC.do"
		
		** Freedom House Media Freedom **
		run "cleaning fh media.do"
		
		** Database of Political institutions
		run "cleaning dpi.do"
		
		** Polity IV **
		run "cleaning polity.do"
		
		** Alesina et al. 2003 **
		run "cleaning Alesina et al 2003.do"
		
		** CIRI and CI Rights **
		run "cleaning CI rights.do"
		
		** IPI Peacekeeping **
		run "cleaning IPI.do"
		
		** Neumayer 2006 **
		run "cleaning Neumayer 2006.do"
		
		** La Porta et al. 1998 **
		run "cleaning La Porta et al 1998.do"
		
		** CIA Factbook **
		run "cleaning CIA.do"
		
		** World Governance Indicators **
		run "cleaning wgi.do"
		
		** Cho Dreher & Neumayer 2013 **
		run "cleaning Cho et al 2013.do"
		
		** Cho's 3P Index **
		run "cleaning 3P.do"
		
		** ICRG **
		run "cleaning ICRG.do"
		
		****************	
		** WORLD BANK **	
			
		** this bit requires downloading the Stata module wbopendata, which can 
		** be done by typing "net install wbopendata" in the Stata command line
			 
		wbopendata, language(en - English) indicator(ny.gdp.mktp.kd  ) long clear
		rename countryname Country
		run "cow.do"
		drop if ccode==0
		collapse (sum) ny_gdp_mktp_kd, by(ccode year) 
		run "ccode to name.do"
		run "creating WB region codes.do"
				
		order ccode country year region
		
		gen africa_es=1 if region==1
		replace africa_es=0 if africa_es~=1
		label var africa_es "Africa, East and Southern"
		
		gen africa_wc=1 if region==2
		replace africa_wc=0 if africa_wc~=1		
		label var africa_wc "Africa, Western and Central"		
		
		gen easia_pacific=1 if region==3
		replace easia_pacific=0 if easia_pacific~=1		
		label var easia_pacific "East Asia and Pacific"	
		
		gen sasia=1 if region==4
		replace sasia=0 if sasia~=1		
		label var sasia "South Asia"	
		
		gen europe_ec=1 if region==5
		replace europe_ec=0 if europe_ec~=1		
		label var europe_ec "Europe, Eastern and Central"	
		
		gen europe_w=1 if region==6
		replace europe_w=0 if europe_w~=1		
		label var europe_w "Europe, Western"	
		
		gen middleeast=1 if region==7
		replace middleeast=0 if middleeast~=1		
		label var middleeast "Middle East"	
		
		gen nafrica=1 if region==8
		replace nafrica=0 if nafrica~=1		
		label var nafrica "North Africa"	
		
		gen americas=1 if region==9
		replace americas=0 if americas~=1		
		label var americas "Americas"	
				 
					******************************
					** MERGING IN OTHER SOURCES **
					******************************
	
	** DREHER et al 2006 **
		merge 1:1 ccode year using "KOF info index 2019.dta" 
		drop _merge
	
	** OECD **
		** 3. Transition economy ** COULD NOT FIND IT.
		** 21. OECD membership	
		run "oecd.do"
 
	* QoG **
		merge 1:1 ccode year using "rel_lan.dta"
		drop _merge 
		
	** Intl country risk guide; kaufmann et al 2009)**	
		** 7. Rule of law  **
		merge 1:1 ccode year using	"FH 2019.dta"
		drop _merge
		** 8. Control of corruption  **	
		** taken from TI 
		rename cpi consumerpriceindex
		merge 1:1 ccode year using	 "cpi_98_18.dta"
		drop _merge
		
	** UNITED NATIONS **
		** 11. Crime rate **	
		** Using homicides instead of general crime **
		merge 1:1 ccode year using	 "homicides.dta"
		drop _merge 	
		merge 1:1 ccode year using	"heroin.dta"
		drop _merge
		
	** Mayer and Zignago (2011) **
		merge m:1 ccode using	"landlocked.dta"
		drop _merge
	
	** Freedom house media freedom **
		merge 1:1 ccode year using "FH media 2017.dta"
		drop _merge
	
	** Database of Political Institutions:
		merge 1:1 ccode year using  "dpi2017 left right.dta"
		drop _merge
	
	* Polity IV *
		merge 1:1 ccode year using "Polity 2018.dta"
		drop _merge
			
	* Alesina 
		merge m:1 ccode  using  "Alesina et al 2003.dta"
		drop _merge
		
	** CI Rights 	
		merge 1:1 ccode year using  "CIRIGHTS 2019.dta"	
		drop _merge
	
	** Peacekeepers **
		merge 1:1 ccode year using 	"IPI_peacekeeping.dta"	
		drop _merge
		merge 1:1 ccode year using 	"PI_peacekeeping_dest.dta"	
		drop _merge
		
	** Visas **
		merge m:1 ccode using 	"Neumayer_visas.dta"
		drop _merge
 
	** Legal origin **
		merge m:1 ccode using "La Porta et al 1998.dta"
		drop _merge
	
	** Major languages
		merge m:1 ccode using "major language.dta"
		drop _merge
	
	** Political stability and voice 
		merge 1:1 ccode year using "wgi_voice_stab.dta"
		drop _merge
	
	** legal prostitution
		merge m:1 ccode using "Cho_pros.dta"
		drop _merge
	
	** 3P index **
		merge 1:1 ccode year using  "data3P_2015.dta"
		drop _merge	
	
	** ICRG conflict **
		merge 1:1 ccode year using "ICRG_conflict.dta"
		drop _merge
	
		drop if year<1996

					************************************
					** PULL FACTOR VARIABLES 		  **
					** checking that they merged okay **
					************************************
					
	* GDP pc
		replace ny_gdp_mktp_kd=. if ny_gdp_mktp_kd==0
		gen gdppc= ny_gdp_mktp_kd/population
		*hist gdppc
		gen gdppcl=ln(gdppc)
		*hist gdppcl
		label var gdppcl "GDP pc, constant 2000 (WB2020)"
		
	** population **
	*hist population
	gen pop=ln(population)
	label var pop "Population, ln (WB2020)"
	* hist pop 
	
	/* Democracy, Polity 2 **
	* Corrption TI cpi
	hist cpi
	
	*Rule of law
	hist rle
	
	*political stability voice and accountability
	hist pve
	hist vae
	
	* external conflict, internal conflict, ethnic tension
	
	hist conflict_external 
	hist conflict_internal 
	hist ethnic_tension
	
	* eth fract, rel fract, language frac
	hist ethnic 
	hist language 
	hist religion
	
	* leftwing exec, rightwing exec
	hist execleft 
	hist execright
	
	*media freeom
	hist mediafreedom
	
	* prost law
	hist prostitutionlaw
	
	*heroin */
	*hist heroin
	gen heroinl=ln(heroin)
	label var heroinl "Heroin seized, ln (UNODC)"
	
	/* women economic rights/ 	womens social rights
	hist wecon 
	hist wosoc
	
	*Social globalisation
	hist KOFSoGI
	
	*Unemployment rate 
	hist unemployment */
	replace unemployment=. if unemployment==0

	*% of workforce in agriculture
	*hist agr_employment
	
	* Literacy rate 
	*hist literacy
	replace literacy=. if literacy==0
	
	*Mortality rate under five
	*hist mortality_under5
	
	*Infant mortality rate 
	
	*Refugees (countries of asylum)
	*hist refugees_d
	gen refugees_dl=ln(refugees_d)
	*hist refugees_dl
	label var refugees_dl "Refugees, asylum country (WB)"
	
	*Share of migrants in population
	*hist migration_net
	
	*Share of age group 65 or above in total populations 
	*International tourism (number of departure) 
	*hist tourism_dep
	gen tourism=ln(tourism_dep)
	label var tourism "Intl tourism departures, ln (WB)"
	
	*Urbanization
	*hist urbanpop
	replace urbanpop=100 if urbanpop>100 & urbanpop~=.
	
	*Visa restriction
	*hist visa_dest
	
	* Trade (share in GDP)
	*hist trade
	
	* FDI (share in GDP) 
	*hist fdi
	count if fdi==0
	replace fdi=. if fdi==0
	
	*hist fdi_gdp
	gen fdi_gdpl=ln(fdi_gdp)
	*hist fdi_gdpl
	
	*Share of food, beverage and tobacco industries in GDP 
	*hist food_bev
	replace food_bev=. if food_bev<0
	
	* Energy use 
	*hist energyuse
	label var energyuse "Energy use "
	
	*Anti-trafficking Policy 
	*hist prevention
	rename prevention prevention_3p
	
	* Grant amnesty (protection policy) 
	*hist protection
	rename protection protection_3p
	
	*Crime rate
	*hist homicides
		
	*Number of UN peacekeepers sent abroad, normalized by populations 
	 *hist peacekeepers
	gen peacekeepersl=ln(peacekeepers)
	label var peacekeepersl "# peacekeepers sent abroad, ln (IPI)"
	
	*Number of UN peacekeepers residing in the country, normalized by populations 
	*hist peacekeepers_foreign
	gen peacekeepersfl=ln(peacekeepers_foreign)
	label var peacekeepersfl "# peacekeepers in country, ln (IPI)"
		
	* OECD membership (dummy)
	*hist oecd

	* Regional dummy: East Asia and Pacific, Europe and Central Asia, Latin America and Caribbean, North Africa and Middle East, North America, South Asia, and Sub- Saharan Africa (7 variables)

	gen ssa=0
	replace ssa=1 if africa_es==1
	replace ssa=1 if africa_wc==1
	label var ssa "Sub-Saharan Africa dummy"
	*hist ssa

	gen europe=0
	replace europe=1 if europe_ec
	replace europe=1 if	europe_w
	label var europe "Europe dummy"
	*hist europe
	
	*hist easia_pacific
	*hist  sasia 
	
	gen mena=0
	replace mena=1 if nafrica==1
	replace mena=1 if middleeast==1
	label var mena "Middle East and N Africa dummy"
	*hist mena
	
	*hist americas   
	
	gen america_n=0
	replace america_n=1 if ccode==2
	replace america_n=1 if ccode==20
	label var america_n "North America dummy"
	
	rename americas america_l
	replace america_l=0 if ccode==2
	replace america_l=0 if ccode==20
	label var america_l "Latin America & Caribbean"
	
	
	/* Share of Religion in populations: Muslim, Catholic, and Protestants (3 variables)

	hist lp_catho80 
	hist lp_muslim80 
	hist lp_protmg80
	
	* Legal origin: British, socialist, French, and German dummy (4 variables)
	
	hist legal_british 
	hist legal_french 
	hist legal_german 
	hist legal_scan
	
	
	* Major language: English, French, Spanish, Portuguese, and German dummy (5 variables) Transition economy (dummy) Landlocked country (dummy)
	
	hist english 
	hist french 
	hist german 
	hist portuguese 
	hist spanish */
	
	**. keeping essential variables 
	keep ccode country year ///
	gdppcl pop polity2 cpi rle ///
	pve vae conflict_external conflict_internal ethnic_tension ///
	ethnic fertility language religion execleft  execright ///
	mediafreedom prostitutionlaw heroinl wecon wosoc ///
	KOFSoGI unemployment agr_employment literacy mortality_under5 ///
	infant_mortality  migration_net refugees_dl tourism urbanpop ///
	visa_dest trade fdi_gdpl food_bev energyuse ///
	prevention_3p protection_3p homicides peacekeepersl peacekeepersfl ///
	oecd ssa europe easia_pacific sasia ///
	mena america_n america_l lp_catho80 lp_muslim80 lp_protmg80 ///
	legal_british legal_french 	legal_german legal_scan ///
	english french german spanish portuguese landlocked
	
	** droppoing countries that didnt exist in 1996
	drop if ccode==265
	drop if ccode==260	
	drop if ccode==315
	drop if ccode==678
	drop if ccode==680
	replace country="Taiwan" if ccode==713

	** filling in gaps in data **
	 	
	browse country ccode year if easia_pacific==.
	replace easia_pacific=0 if country=="Serbia"
	replace easia_pacific=0 if country=="Montenegro"
	replace easia_pacific=1 if country=="Taiwan"	

	replace sasia=0 if country=="Serbia"
	replace sasia=0 if country=="Montenegro"
	replace sasia=0 if country=="Taiwan"		

	replace oecd=0 if country=="Serbia"
	replace oecd=0 if country=="Montenegro"
	replace oecd=0 if country=="Taiwan"	
	
	*Kosovo	347
	replace landlocked=1 if ccode==347
	*Monaco	221
	replace landlocked=0 if ccode==221
	*Liechtenstein	223
	replace landlocked=1 if ccode==223
	*Montenegro	341
	replace landlocked=0 if ccode==341
 
	*Montenegro	348
	drop if ccode==348
	
	*South Sudan	626
	replace landlocked=1 if ccode==626

	save "HT EBA IVs.dta", replace
	
	
	
	
		*********************************************		
		** need to average IV values for 1996-2003 **
		*********************************************
		
		sort ccode year
		
		*1-5*
		gen gdpave = (gdppcl[_n-3] + gdppcl[_n-4] + gdppcl[_n-5] + gdppcl[_n-6] + gdppcl[_n-7]  + gdppcl[_n-8]  + gdppcl[_n-9]  + gdppcl[_n-10])/8  if year==2006
		drop gdppcl
		
		gen popave = (pop[_n-3] + pop[_n-4] + pop[_n-5] + pop[_n-6] + pop[_n-7]  + pop[_n-8]  + pop[_n-9]  + pop[_n-10])/8  if year==2006
		drop pop
	
		gen KOFSoGIave = (KOFSoGI[_n-3] + KOFSoGI[_n-4] + KOFSoGI[_n-5] + KOFSoGI[_n-6] + KOFSoGI[_n-7]  + KOFSoGI[_n-8]  + KOFSoGI[_n-9]  + KOFSoGI[_n-10])/8  if year==2006
		drop KOFSoGI
		
		gen foodave = (food_bev[_n-3] + food_bev[_n-4] + food_bev[_n-5] + food_bev[_n-6] + food_bev[_n-7]  + food_bev[_n-8]  + food_bev[_n-9]  + food_bev[_n-10])/8  if year==2006
		drop food_bev
		
		gen muslimave = (lp_muslim80[_n-3] + lp_muslim80[_n-4] + lp_muslim80[_n-5] + lp_muslim80[_n-6] + lp_muslim80[_n-7]  + lp_muslim80[_n-8]  + lp_muslim80[_n-9]  + lp_muslim80[_n-10])/8  if year==2006
		drop lp_muslim80
		
		*6-10*
		gen cpiave = (cpi[_n-3] + cpi[_n-4] + cpi[_n-5] + cpi[_n-6] + cpi[_n-7]  + cpi[_n-8]   )/6 if year==2006
		drop cpi
		 
		gen infantave = (infant_mortality[_n-3] + infant_mortality[_n-4] + infant_mortality[_n-5] + infant_mortality[_n-6] + infant_mortality[_n-7]  + infant_mortality[_n-8]  + infant_mortality[_n-9]  + infant_mortality[_n-10])/8  if year==2006		 
		drop infant_mortality
			 
		gen agrave= (agr_employment[_n-3] + agr_employment[_n-4] + agr_employment[_n-5] + agr_employment[_n-6] + agr_employment[_n-7]  + agr_employment[_n-8]  + agr_employment[_n-9]  + agr_employment[_n-10])/8  if year==2006
		drop agr_employment
		 
		gen homicidesave = (homicides[_n-3] + homicides[_n-4] + homicides[_n-5] + homicides[_n-6])/4  if year==2006
		drop homicides
	
		gen conflict_extave= (conflict_external[_n-3] + conflict_external[_n-4] + conflict_external[_n-5] + conflict_external[_n-6] + conflict_external[_n-7]  + conflict_external[_n-8]  + conflict_external[_n-9]  + conflict_external[_n-10])/8  if year==2006	
		drop conflict_external
	
		*11-15
		gen conflict_intave= (conflict_internal[_n-3] + conflict_internal[_n-4] + conflict_internal[_n-5] + conflict_internal[_n-6] + conflict_internal[_n-7]  + conflict_internal[_n-8]  + conflict_internal[_n-9]  + conflict_internal[_n-10])/8  if year==2006	
		drop conflict_internal	
	
		gen energyuseave= (energyuse[_n-3] + energyuse[_n-4] + energyuse[_n-5] + energyuse[_n-6] + energyuse[_n-7]  + energyuse[_n-8]  + energyuse[_n-9]  + energyuse[_n-10])/8  if year==2006	
		drop energyuse	
	
		gen ethnicave= (ethnic[_n-3] + ethnic[_n-4] + ethnic[_n-5] + ethnic[_n-6] + ethnic[_n-7]  + ethnic[_n-8]  + ethnic[_n-9]  + ethnic[_n-10])/8  if year==2006	
		drop ethnic	
			
		gen ethnic_tensionave= (ethnic_tension[_n-3] + ethnic_tension[_n-4] + ethnic_tension[_n-5] + ethnic_tension[_n-6] + ethnic_tension[_n-7] + ethnic_tension[_n-8] + ethnic_tension[_n-9] + ethnic_tension[_n-10])/8  if year==2006	
		drop ethnic_tension			
	
		gen execleftave= (execleft[_n-3] + execleft[_n-4] + execleft[_n-5] + execleft[_n-6] + execleft[_n-7]  + execleft[_n-8]  + execleft[_n-9]  + execleft[_n-10])/8  if year==2006	
		drop execleft			
		
		*16-20
		gen execrightave= (execright[_n-3] + execright[_n-4] + execright[_n-5] + execright[_n-6] + execright[_n-7]  + execright[_n-8]  + execright[_n-9]  + execright[_n-10])/8  if year==2006	
		drop execright			
		
		gen fdiave= (fdi_gdpl[_n-3] + fdi_gdpl[_n-4] + fdi_gdpl[_n-5] + fdi_gdpl[_n-6] + fdi_gdpl[_n-7]  + fdi_gdpl[_n-8]  + fdi_gdpl[_n-9]  + fdi_gdpl[_n-10])/8  if year==2006	
		drop fdi_gdpl			
		
		* heroin only available 2012 and later
		drop heroinl
	
		gen languageave= (language[_n-3] + language[_n-4] + language[_n-5] + language[_n-6] + language[_n-7]  + language[_n-8]  + language[_n-9]  + language[_n-10])/8  if year==2006		
		drop language
	
		gen legal_britishave= (legal_british[_n-3] + legal_british[_n-4] + legal_british[_n-5] + legal_british[_n-6] + legal_british[_n-7]  + legal_british[_n-8]  + legal_british[_n-9]  + legal_british[_n-10])/8  if year==2006	
		drop legal_british

		*21-25
		gen legal_frenchave= (legal_french[_n-3] + legal_french[_n-4] + legal_french[_n-5] + legal_french[_n-6] + legal_french[_n-7]  + legal_french[_n-8]  + legal_french[_n-9]  + legal_french[_n-10])/8  if year==2006	
		drop legal_french	
	
		gen legal_germanave= (legal_german[_n-3] + legal_german[_n-4] + legal_german[_n-5] + legal_german[_n-6] + legal_german[_n-7]  + legal_german[_n-8]  + legal_german[_n-9]  + legal_german[_n-10])/8  if year==2006	
		drop legal_german	
	
		gen legal_scanave= (legal_scan[_n-3] + legal_scan[_n-4] + legal_scan[_n-5] + legal_scan[_n-6] + legal_scan[_n-7]  + legal_scan[_n-8]  + legal_scan[_n-9]  + legal_scan[_n-10])/8  if year==2006	
		drop legal_scan
	
		gen litlag=literacy[_n-1] if literacy==. & literacy[_n-1]~=.
		replace litlag=literacy[_n-2] if literacy==. & literacy[_n-2]~=.
		replace litlag=literacy[_n-3] if literacy==. & literacy[_n-3]~=.
		replace litlag=literacy[_n-4] if literacy==. & literacy[_n-4]~=.
		replace litlag=literacy[_n-5] if literacy==. & literacy[_n-5]~=.
		replace litlag=literacy[_n-6] if literacy==. & literacy[_n-6]~=.
		replace litlag=literacy[_n-7] if literacy==. & literacy[_n-7]~=.
		replace litlag=literacy[_n-8] if literacy==. & literacy[_n-8]~=.
		replace litlag=literacy[_n-9] if literacy==. & literacy[_n-9]~=.							
		replace litlag=literacy[_n-10] if literacy==. & literacy[_n-10]~=.	
		replace litlag=literacy if litlag==. & literacy~=.
		gen literacyave= (litlag[_n-3] + litlag[_n-4] + litlag[_n-5] + litlag[_n-6] + litlag[_n-7]  + litlag[_n-8]  + litlag[_n-9]  + litlag[_n-10])/8  if year==2006	
		drop literacy litlag
	
		gen lp_catho80ave= (lp_catho80[_n-3] + lp_catho80[_n-4] + lp_catho80[_n-5] + lp_catho80[_n-6] + lp_catho80[_n-7] + lp_catho80[_n-8]  + lp_catho80[_n-9]  + lp_catho80[_n-10])/8  if year==2006	
		drop lp_catho80	
		
		*26-30
		gen lp_protmg80ave= (lp_protmg80[_n-3] + lp_protmg80[_n-4] + lp_protmg80[_n-5] + lp_protmg80[_n-6] + lp_protmg80[_n-7]  + lp_protmg80[_n-8]  + lp_protmg80[_n-9]  + lp_protmg80[_n-10])/8  if year==2006	
		drop lp_protmg80	
			
		gen mediaave= (mediafreedom[_n-3] + mediafreedom[_n-4] + mediafreedom[_n-5] + mediafreedom[_n-6])/4  if year==2006	
		drop mediafreedom	
		
		gen migrationave= ( migration_net[_n-4] + migration_net[_n-9])/2  if year==2006	
		drop migration_net	
			
		gen mortality5ave= (mortality_under5[_n-3] + mortality_under5[_n-4] + mortality_under5[_n-5] + mortality_under5[_n-6] + mortality_under5[_n-7]  + mortality_under5[_n-8]  + mortality_under5[_n-9]  + mortality_under5[_n-10])/8  if year==2006	
		drop mortality_under5
	
		replace peacekeepersfl=0 if peacekeepersfl==.
		gen peacekeepersflave= (peacekeepersfl[_n-3] + peacekeepersfl[_n-4] + peacekeepersfl[_n-5] + peacekeepersfl[_n-6] + peacekeepersfl[_n-7]  + peacekeepersfl[_n-8]  + peacekeepersfl[_n-9]  + peacekeepersfl[_n-10])/8  if year==2006	
		drop peacekeepersfl
	
		*31-35
		replace peacekeepersl=0 if peacekeepersl==.
		gen peacekeeperslave= (peacekeepersl[_n-3] + peacekeepersl[_n-4] + peacekeepersl[_n-5] + peacekeepersl[_n-6] + peacekeepersl[_n-7]  + peacekeepersl[_n-8]  + peacekeepersl[_n-9]  + peacekeepersl[_n-10])/8  if year==2006	
		drop peacekeepersl
		
		gen polity2ave= (polity2[_n-3] + polity2[_n-4] + polity2[_n-5] + polity2[_n-6] + polity2[_n-7]  + polity2[_n-8]  + polity2[_n-9]  + polity2[_n-10])/8  if year==2006		
		drop polity2
		
		gen preventlag=prevention_3p[_n-1] if prevention_3p==. & prevention_3p[_n-1]~=.
		replace preventlag=prevention_3p[_n-2] if prevention_3p==. & prevention_3p[_n-2]~=.
		replace preventlag=prevention_3p[_n-3] if prevention_3p==. & prevention_3p[_n-3]~=.
		replace preventlag=prevention_3p[_n-4] if prevention_3p==. & prevention_3p[_n-4]~=.
		replace preventlag=prevention_3p[_n-5] if prevention_3p==. & prevention_3p[_n-5]~=.
		replace preventlag=prevention_3p[_n-6] if prevention_3p==. & prevention_3p[_n-6]~=.
		replace preventlag=prevention_3p[_n-7] if prevention_3p==. & prevention_3p[_n-7]~=.
		replace preventlag=prevention_3p[_n-8] if prevention_3p==. & prevention_3p[_n-8]~=.
		replace preventlag=prevention_3p[_n-9] if prevention_3p==. & prevention_3p[_n-9]~=.					
		replace preventlag=prevention_3p[_n-10] if prevention_3p==. & prevention_3p[_n-10]~=.	
		replace preventlag=prevention_3p if preventlag==. & prevention_3p~=.
		gen preventave= (preventlag[_n-3] + preventlag[_n-4] + preventlag[_n-5] + preventlag[_n-6])/4  if year==2006		
		drop prevention_3p
		drop preventlag
		
		gen protectlag=protection_3p[_n-1] if protection_3p==. & protection_3p[_n-1]~=.
		replace protectlag=protection_3p[_n-2] if protection_3p==. & protection_3p[_n-2]~=.
		replace protectlag=protection_3p[_n-3] if protection_3p==. & protection_3p[_n-3]~=.
		replace protectlag=protection_3p[_n-4] if protection_3p==. & protection_3p[_n-4]~=.
		replace protectlag=protection_3p[_n-5] if protection_3p==. & protection_3p[_n-5]~=.
		replace protectlag=protection_3p[_n-6] if protection_3p==. & protection_3p[_n-6]~=.
		replace protectlag=protection_3p[_n-7] if protection_3p==. & protection_3p[_n-7]~=.
		replace protectlag=protection_3p[_n-8] if protection_3p==. & protection_3p[_n-8]~=.
		replace protectlag=protection_3p[_n-9] if protection_3p==. & protection_3p[_n-9]~=.					
		replace protectlag=protection_3p[_n-10] if protection_3p==. & protection_3p[_n-10]~=.	
		replace protectlag=protection_3p if protectlag==. & protection_3p~=.
		gen protectave= (protectlag[_n-3] + protectlag[_n-4] + protectlag[_n-5] + protectlag[_n-6])/4  if year==2006	
		drop protection_3p
		drop protectlag
			
		gen prostlawave= (prostitutionlaw[_n-3] + prostitutionlaw[_n-4] + prostitutionlaw[_n-5] + prostitutionlaw[_n-6] + prostitutionlaw[_n-7]  + prostitutionlaw[_n-8]  + prostitutionlaw[_n-9]  + prostitutionlaw[_n-10])/8  if year==2006	
		drop prostitutionlaw	
		
		*36-40
		gen pveave= (pve[_n-3] + pve[_n-4]  + pve[_n-6] + pve[_n-8]   + pve[_n-10])/5  if year==2006			
		drop pve	
		
		gen refugeesave= (refugees_dl[_n-3] + refugees_dl[_n-4] + refugees_dl[_n-5] + refugees_dl[_n-6] + refugees_dl[_n-7]  + refugees_dl[_n-8]  + refugees_dl[_n-9]  + refugees_dl[_n-10])/8  if year==2006		
		drop refugees_dl
		
		gen religionave= (religion[_n-3] + religion[_n-4] + religion[_n-5] + religion[_n-6] + religion[_n-7]  + religion[_n-8]  + religion[_n-9]  + religion[_n-10])/8  if year==2006		
		drop religion

		gen rleave= (rle[_n-3] + rle[_n-4]  + rle[_n-6] + rle[_n-8] + rle[_n-10])/5  if year==2006			
		drop rle
		
		gen tourismave= (tourism[_n-3] + tourism[_n-4] + tourism[_n-5] + tourism[_n-6] + tourism[_n-7]  + tourism[_n-8]  + tourism[_n-9]  + tourism[_n-10])/8  if year==2006		
		drop tourism		
		
		*41-45
		gen tradeave= (trade[_n-3] + trade[_n-4] + trade[_n-5] + trade[_n-6] + trade[_n-7]  + trade[_n-8]  + trade[_n-9]  + trade[_n-10])/8  if year==2006		
		drop trade
		
		gen unemploymentave= (unemployment[_n-3] + unemployment[_n-4] + unemployment[_n-5] + unemployment[_n-6] + unemployment[_n-7]  + unemployment[_n-8]  + unemployment[_n-9]  + unemployment[_n-10])/8  if year==2006	
		drop unemployment	
	
		gen urbanave= (urbanpop[_n-3] + urbanpop[_n-4] + urbanpop[_n-5] + urbanpop[_n-6] + urbanpop[_n-7]  + urbanpop[_n-8]  + urbanpop[_n-9]  + urbanpop[_n-10])/8  if year==2006	
		drop urbanpop	

		gen vaeave= (vae[_n-3] + vae[_n-4]  + vae[_n-6] + vae[_n-8] + vae[_n-10])/5  if year==2006		
		drop vae

		gen visaave= (visa_dest[_n-3] + visa_dest[_n-4] + visa_dest[_n-5] + visa_dest[_n-6] + visa_dest[_n-7]  + visa_dest[_n-8]  + visa_dest[_n-9]  + visa_dest[_n-10])/8  if year==2006			
		drop visa_dest	

		*46-
		gen weconave= (wecon[_n-3] + wecon[_n-4] + wecon[_n-5] + wecon[_n-6] + wecon[_n-7]  + wecon[_n-8] + wecon[_n-9]  + wecon[_n-10])/8  if year==2006			
		drop wecon	
		
		gen wosocave= (wosoc[_n-3] + wosoc[_n-4] + wosoc[_n-5] + wosoc[_n-6] + wosoc[_n-7]  + wosoc[_n-8] + wosoc[_n-9]  + wosoc[_n-10])/8  if year==2006			
		drop wosoc		
		
		keep if year==2006
			
		order ccode country year

		save "HT EBA IVs 2006.dta", replace
	
	
			/*2005 -1
			4 -2
			3 -3
			2 -4
			1 -5
			0 -6
			9 -7
			8 -8
			7 -9
			6 -10	*/
							
				*************************
				** DEPENDENT VARIABLES **				
				*************************				
			
		** 2006 **
		
		run "cleaning UNODC 2006.do"
		
		use "HTI 00-17.dta", clear
		replace ccode=732 if ccode==730
		merge 1:1 ccode year using "UNODC_2006_std.dta"
		drop _merge
		merge 1:1 ccode year using	"HT EBA IVs 2006.dta"
		drop if ccode==-33
		drop if ccode==-22
		drop if ccode==-11
		drop if year<1996	
		replace oecd=0 if ccode==265
		drop _merge
		keep if year==2006
		
		drop CEDAW_rat CEDAW_sign ILO105 ILO182 ILO29 INTERNATIONAL_LAWS PREVENTION PROSECUTION PROTECTION TRAFFICKING_FLOWS TRAFFICKING_TYPES UNCRC_rat UNCRC_sign UNP_rat UNP_sign conflict_rat conflict_sign cpinternal cpsource cptransit csdest csinternal csource cssource cstransit ctransit domesticlaws dinternal dsdest dsinternal dsource dssource dstransit dtransit protocol protectprogress prosnum progress preventprogress cdest cinternal cldest clinternal clsource cltransit convictinfo cpdest ddest efforts enforcement internal linternal ltransit lsource minstand pinternal ptransit psource tier source_order transit_UNODC transit_order transit  destination_order victimid victimpunish victimservices
			
		replace country=Country if country=="" & Country ~=""
		drop Country
		
		replace easia_pacific=0 if country=="Serbia"
		replace easia_pacific=0 if country=="Montenegro"
		replace easia_pacific=1 if country=="Taiwan"	
		
		replace sasia=0 if country=="Serbia"
		replace sasia=0 if country=="Montenegro"
		replace sasia=0 if country=="Taiwan"		

		replace oecd=0 if country=="Serbia"
		replace oecd=0 if country=="Montenegro"
		replace oecd=0 if country=="Taiwan"	
 
		save "HT EBA merged DV and IVs 2006.dta", replace	

		***************	
		** All years **
		
		use "HTI 00-17.dta", clear
		replace ccode=732 if ccode==730
		merge 1:1 ccode year using	"HT EBA IVs.dta"
		drop if ccode==-33
		drop if ccode==-22
		drop if ccode==-11	
		drop if year<1996	
		replace oecd=0 if ccode==265		
		drop if ccode==-33
		drop if ccode==-22
		drop if ccode==-11	
		drop if year<1996	
		replace oecd=0 if ccode==265
		drop _merge
		
		drop CEDAW_rat CEDAW_sign ILO105 ILO182 ILO29 INTERNATIONAL_LAWS PREVENTION PROSECUTION PROTECTION TRAFFICKING_FLOWS TRAFFICKING_TYPES UNCRC_rat UNCRC_sign UNP_rat UNP_sign conflict_rat conflict_sign cpinternal cpsource cptransit csdest csinternal csource cssource cstransit ctransit domesticlaws dinternal dsdest dsinternal dsource dssource dstransit dtransit protocol protectprogress prosnum progress preventprogress cdest cinternal cldest clinternal clsource cltransit convictinfo cpdest ddest efforts enforcement internal linternal ltransit lsource minstand pinternal ptransit psource tier source_order   transit_order transit  destination_order victimid victimpunish victimservices
		
		replace country=Country if country=="" & Country ~=""
		drop Country
	
		replace easia_pacific=0 if country=="Serbia"
		replace easia_pacific=0 if country=="Montenegro"
		replace easia_pacific=1 if country=="Taiwan"	
		
		replace sasia=0 if country=="Serbia"
		replace sasia=0 if country=="Montenegro"
		replace sasia=0 if country=="Taiwan"		

		replace oecd=0 if country=="Serbia"
		replace oecd=0 if country=="Montenegro"
		replace oecd=0 if country=="Taiwan"	

		tsset ccode year
				
		keep if year>1999
		keep if year<2018
		
		replace peacekeepersfl=0 if peacekeepersfl==.
		replace peacekeepersl=0 if peacekeepersl==.
			
		sort ccode year
		by ccode: replace literacy=literacy[_n-1] if literacy==. & literacy[_n-1]~=.
		by ccode: replace migration_net=migration_net[_n-1] if migration_net==. & migration_net[_n-1]~=.
		
		* wosoc discontinued in 2005
	
		save "HT EBA merged DV and IVs.dta", replace	
			
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	
